package com.mew.roller.util;

import cn.hutool.core.util.RandomUtil;
import com.mew.common.util.TxtUtils;

import java.io.File;

/**
 * @author dylin
 * @create 2021/5/31
 */
public class RandomRollerUtil {
    private static final String roller_path="/external/roller/";
    /**
     * @param row 多少行
     * @param col 多少列
     * @param icon 图标个数
     * @param length 长度
     * @param game 游戏
     * @param free 免费图标  -1不替换
     * @return
     */
    public static void randomRoller(int row,int col,int icon,int length,String game,int free){
        System.out.println("清除历史文件...");
        String[] xmls=removeFile(game);

        for (String xml : xmls) {
            System.out.println("生成文件："+xml.replace("\\","/").substring(1));
            boolean isFree=xml.indexOf("charge")>0;
            StringBuffer sb=new StringBuffer();
            sb.append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>").append("\n");
            sb.append("<Config>").append("\n");
            for (int r = 1; r < row+1; r++) {
                sb.append("\t<Roller RollerID = \""+r+"\">").append("\n");
                for (int c = 1; c < col+1; c++) {
                    sb.append("\t\t<Line ID = \""+c+"\" Size = \""+length+"\">").append("\n").append("\t\t\t<Array>").append("\n");
                    sb.append("\t\t\t\t\"");
                    for (int l = 0; l < length; l++) {
                        int rnd=RandomUtil.randomInt(icon);
                        if(isFree){
                            rnd=rnd>0&&free==rnd?0:rnd;
                        }
                        if(l==0){
                            sb.append(rnd);
                        }else{
                            sb.append(",").append(rnd);
                        }
                    }
                    sb.append("\"").append("\n").append("\t\t\t</Array>").append("\n").append("\t\t</Line>").append("\n");
                }
                sb.append("\t</Roller>").append("\n");
            }
            sb.append("</Config>");
            TxtUtils.stringOut(xml,sb.toString());
        }
        System.out.println("结束");
    }
    private static String[] removeFile(String name){
        String[] xmls=new String[]{
                roller_path+name+File.separator+name+"_roller.xml",roller_path+name+File.separator+name+"_charge_roller.xml"
        };
        String rootPath=System.getProperty("user.dir");
        for (String xml : xmls) {
            String path = rootPath+ File.separator+xml ;
            File f=new File(path);
            if(f.exists()){
                f.delete();
            }
        }

        return xmls;
    }
    public static void main(String[] args) {
        RandomRollerUtil.randomRoller(5,5,14,50,"jdqe",11);
    }
}
